package starComponent.elements;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

import starComponent.E_PossibleBranches;
import starComponent.controls.StarComponentControler;
import data.visu.utils.Numbers;

/**
 * @author Charles Perin
 * 9 juin 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class FlyingNameLabelComparator extends FlyingNameLabel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 9104240015392776287L;


	private String value2 = "";


	private A_StarBranch branch2;


	public FlyingNameLabelComparator(FlyingNameLabel flyingNameLabel, StarComponentControler star2) {
		super(flyingNameLabel);
		ArrayList<A_StarBranch> listBranch2 = star2.getAllPossibleBranchList();
		for(A_StarBranch b: listBranch2){
			if(b.getName().equals(flyingNameLabel.getBranch().getName())){
				branch2 = b;
				if(branch2 instanceof StarBranchSlider){
					((StarBranchSlider)b).getThumb().addObserverThumb(this);
				}
				else if(branch2 instanceof StarBranchBiSlider){
					((StarBranchBiSlider)b).getThumbStart().addObserverThumb(this);
					((StarBranchBiSlider)b).getThumbEnd().addObserverThumb(this);
				}
				else if(b instanceof StarBranchBinaries){
					for(ThumbForMultipleBinaries t:((StarBranchBinaries)b).getThumbsTab()){
						t.addObserverThumb(this);
					}
				}
				break;
			}
		}

	}

	@Override
	public void paintComponent(Graphics g){

		Graphics2D g2d = (Graphics2D)g;

		FontMetrics metrics = g.getFontMetrics();
		int textLength = metrics.stringWidth(getText());
		int valueLength = metrics.stringWidth(value);
		int valueLength2 = metrics.stringWidth(value2);

		//si orientation du popup selon utilisateur
		if(branch.getStar().getControler().isCollaborative()){
			double angle = -branch.getAngle()-Math.PI/2;
			g2d.rotate(angle, this.getWidth()/2, this.getHeight()/2);
		}


		g.drawString(getText(), (this.getWidth()-textLength)/2, (this.getHeight()/2));

		g.setColor(Color.RED);
		g.drawString(value, (this.getWidth()-valueLength)/2, this.getHeight()/2+metrics.getHeight());

		g.setColor(Color.BLUE);
		g.drawString(value2, (this.getWidth()-valueLength2)/2, this.getHeight()/2+2*metrics.getHeight());
	}


	@Override
	protected void calculateValue(){
		String text=branch.getName();
		this.setText(text);

		setValue("");
		setValue2("");
		int branchType = branch.getBranchType().getType();
		if(branch instanceof StarBranchSlider){
			if(branchType==E_PossibleBranches.QUANTITATIVE_SLIDER){
				setValue(Double.toString(Numbers.roundDouble(((StarBranchSlider) branch).getValue(),2)));
				setValue2(Double.toString(Numbers.roundDouble(((StarBranchSlider) branch2).getValue(),2)));
			}
			else if(branchType==E_PossibleBranches.QUALITATIVE_SLIDER){
				setValue(branch.getBranchType().getLabels()[(int) ((StarBranchSlider)branch).getValue()].getString());
				setValue2(branch.getBranchType().getLabels()[(int) ((StarBranchSlider)branch2).getValue()].getString());
			}
		}
		else if(branch instanceof StarBranchBiSlider){
			if(branchType==E_PossibleBranches.QUANTITATIVE_BISLIDER){
				setValue(Double.toString(Numbers.roundDouble(((StarBranchBiSlider) branch).getMinValue(),2))+
						" -> "+
						Double.toString(Numbers.roundDouble(((StarBranchBiSlider) branch).getMaxValue(),2)));
				setValue2(Double.toString(Numbers.roundDouble(((StarBranchBiSlider) branch2).getMinValue(),2))+
						" -> "+
						Double.toString(Numbers.roundDouble(((StarBranchBiSlider) branch2).getMaxValue(),2)));
			}
			else if(branchType==E_PossibleBranches.QUALITATIVE_BISLIDER){
				setValue(branch.getBranchType().getLabels()[(int) ((StarBranchBiSlider)branch).getMinValue()].getString()+
						" -> "+
						branch.getBranchType().getLabels()[(int) ((StarBranchBiSlider)branch).getMaxValue()].getString());
				setValue2(branch.getBranchType().getLabels()[(int) ((StarBranchBiSlider)branch2).getMinValue()].getString()+
						" -> "+
						branch.getBranchType().getLabels()[(int) ((StarBranchBiSlider)branch2).getMaxValue()].getString());
			}
		}
	}

	@Override
	protected double[] calculateLabelPosition(){
		String text=branch.getName();
		double xLabel = branch.getxEnd();
		double yLabel = branch.getyEnd();
		double angle = branch.getAngle();
		if(angle>=0){
			if(angle>15*Math.PI/8 || angle <Math.PI/8){
				xLabel = xLabel+2*MARGIN_LABEL;
				//yLabel = yLabel-MARGIN_LABEL-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=Math.PI/4){
				xLabel = xLabel+3*MARGIN_LABEL;
				yLabel = yLabel-text.length()*7;
			}
			else if(angle<=2*Math.PI/4){
				xLabel = xLabel-text.length()*7/2-3*MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=3*Math.PI/4){
				xLabel = xLabel-text.length()*7-MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=4*Math.PI/4){
				xLabel = xLabel-text.length()*7-MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=5*Math.PI/4){
				xLabel = xLabel - text.length()*7/2;
				yLabel = yLabel+MARGIN_LABEL;
			}
			else if(angle<=6*Math.PI/4){
				xLabel = xLabel - text.length()*7/2+2*MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT/2;
			}
			else if(angle<=7*Math.PI/4){
				xLabel = xLabel -text.length()*7/2+ 2*MARGIN_LABEL;
				yLabel = yLabel - MARGIN_LABEL-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=8*Math.PI/4){
				xLabel = xLabel + MARGIN_LABEL;
				yLabel = yLabel- MARGIN_LABEL-StarComponent.LABEL_HEIGHT;
			}
			else System.err.println("l'angle doit etre inferieur a 2PI");
		}
		else System.err.println("l'angle doit etre superieur a 0");

		double[] res = new double[2];
		res[0] = xLabel;
		res[1] = yLabel;
		return res;
	}

	public A_StarBranch getBranch2() {
		return branch2;
	}

	public void setBranch2(A_StarBranch branch2) {
		this.branch2 = branch2;
	}

	public String getValue2() {
		return value2;
	}

	public void setValue2(String value2) {
		this.value2 = value2;
	}
}
